var express = require('express');
var router = express.Router();
var request = require('request');
var md5 = require('md5');

var CommonJS = require('../../../common/common');
var DBConfig = require('../../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);


// 查询用户openid
router.get('/jkyx/me_openid', function(req, res, next) {
	var param = req.query || req.params; 
	var openid = param.openid || '';
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getOpendiByOpenid,[openid],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				if (results.length > 0) {
					var userInfo = results[0];
					tempuserInfo = {
						id: userInfo.id,
						account_id: userInfo.account_id,
						birthday: CommonJS.formatDate(userInfo.birthday),
						balance: userInfo.balance / 100,
						nickname: userInfo.nickname,
						wx_nickname: userInfo.wx_nickname,
						avatarUrl: userInfo.avatarUrl,
						gender: userInfo.gender,
					}
					res.end(JSON.stringify({
		     			msg: '操作成功',
		     			status:'100',
		     			userInfo: tempuserInfo
		     		}));	 
				} else{
					res.end(JSON.stringify({
		     			msg: '操作成功',
		     			status:'101'
		     		}));	   
				}      
			}
		})
	})
});
// 获取账户余额
router.get('/jkyx/me_balance', function(req, res, next) {
	var param = req.query || req.params; 
	var account_id = param.account_id || '';
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getBalance,[account_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				  res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			money: results[0].balance
	     		})); 
			}
		})
	})
});
// 获取消费记录
router.get('/jkyx/rechange_record', function(req, res, next) {
	var param = req.query || req.params; 
	var account_id = param.account_id || '';
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		var sql = 'select * from rechange_record where account_id = ? order by id desc';
		connection.query(sql,[account_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				  res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			data: results
	     		})); 
			}
		})
	})
});

// 确认支付密码 
router.get('/jkyx/comfon_pw', function(req, res, next) {
	var param = req.query || req.params; 
	var account_id = param.account_id || '';
	var passw = param.passw || ''; 
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getPayPw,[account_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				if (results.length == 0) {
					res.end(JSON.stringify({
			     		msg: '未设置支付密码',
		     			status:'105',
		     		})); 
				} else{
					var pay_password = results[0].pay_password;
					// 密码正确
					if (pay_password == md5(passw)) {
						res.end(JSON.stringify({
			     			msg: '操作成功',
			     			status:'100',
			     		})); 
					} else{ // 密码错误
						res.end(JSON.stringify({
			     			msg: '密码错误',
			     			status:'104',
			     		})); 
					}	
				}
			}
		})
	})
});


// 设置/修改支付密码 
router.get('/jkyx/change_pw', function(req, res, next) {
	var param = req.query || req.params; 
	var account_id = param.account_id || '';
	var passw = param.passw || ''; 
	passw = md5(passw);
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.setPayPW,[passw,account_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
		     		msg: '设置成功',
	     			status:'100',
	     		})); 
			}
		})
	})
});


module.exports = router;
